# -*- coding: utf-8 -*-
import scrapy


class YouhuiSpider(scrapy.Spider):
    name = 'youhui'
    allowed_domains = ['smzdm.com']
    start_urls = ['https://m.smzdm.com/youhui/']
    User_Agent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36'
    num = 1

    def parse(self, response):
        # 获取所需页面数据
        li_list = response.xpath('//ul[@class="card-group-ul clearfix"]/li')
        for i in li_list:
            name = i.xpath('.//img/@alt').extract_first()
            price = i.xpath('.//div[@class="card-price"]/text()').extract_first()
            source = i.xpath('.//span[@class="card-mall"]/text()').extract_first()
            date = i.xpath('.//div[@class="card-actions-left"]//span[2]/text()').extract_first()
            # 传递给管道进行处理
            yield {
                'name': name,
                'price': price.replace(' ', '').replace('\n', ''),
                'source': source,
                'date': date
            }
        # 爬取下一页
        YouhuiSpider.num += 1
        if YouhuiSpider.num < 500:
            new_url = 'https://m.smzdm.com/youhui/' + 'p%s/' % YouhuiSpider.num
            yield scrapy.Request(url=new_url, callback=self.parse)
            print('爬起新页面...', new_url)

# scrapy crawl youhui
